/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.cam.can.evaluation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import uk.ac.cam.can.Data;
import uk.ac.cam.can.misc.MathTools;

/**
 *
 * @author thomas
 */
public class Evaluation {
    public static double computeQuality(Data data){
        Map<String, List<String[]>> qgroups = new HashMap<String, List<String[]>>();
        for(String[] record : data.getRecords()){
            String qi="";
            for(int i=0; i < data.nbAttributes(); i++){
                if(data.getType(i)==Data.TYPE_QUASI_IDENTIFIER)
                    qi+=record[i];
            }
            if(qgroups.containsKey(qi)){
                qgroups.get(qi).add(record);
            }else{
                //System.out.println(qi);
                qgroups.put(qi, new ArrayList<String[]>());
                qgroups.get(qi).add(record);
            }
        }
        
        double averageArea = 0.0;
        for(List<String[]> qgroup : qgroups.values()){
            // all record within a qgroup should have the same size
            String[] record = qgroup.get(0);
            double area=1.0;
            for(int i=0; i < record.length; i++)
                area*=data.getLength(i, record[i]);
            averageArea+=area;
        }
        return averageArea/(double)qgroups.size();
    }
    
    public static double computeWorstQuality(Data data){
        return data.getWorstQuality();
    }
    
    public static double computeRisk(Data data){
        Map<String, List<String[]>> groups = new HashMap<String, List<String[]>>();
        for(String[] record : data.getRecords()){
            String qi="";
            for(int i=0; i < data.nbAttributes(); i++){
                    qi+=record[i];
            }
            if(groups.containsKey(qi)){
                groups.get(qi).add(record);
            }else{
                //System.out.println(qi);
                groups.put(qi, new ArrayList<String[]>());
                groups.get(qi).add(record);
            }
        }
        double entropy = 0.0;
        for(List<String[]> group : groups.values()){
            double frequency = (double)group.size() / (double)data.size();
            entropy-= frequency * MathTools.log2(frequency);
        }
        return Math.pow(2, entropy);
    }
    
    
    
}
